/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [SketchpadFrame.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_gui.sketchpad.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import pkg_02_gui.sketchpad.command.ShapeCommand;

/**
 * 绘图板主窗口
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class SketchpadFrame extends JFrame {

	private static final long serialVersionUID = -7080053971741609904L;

	private final JPanel commandPanel = new JPanel(); // 存放命令的面板
	private final JPanel colorPanel = new JPanel(); // 存放颜色的面板
	private final JPanel mainPanel = new JPanel(); // 主面板

	private final JButton redButton = new JButton("红色");
	private final JButton blueButton = new JButton("蓝色");
	private final JButton greenButton = new JButton("绿色");

	private final JButton lineButton = new JButton("直线");
	private final JButton circleButton = new JButton("圆");
	private final JButton rectangleButton = new JButton("矩形");

	private final JButton undoButton = new JButton("撤销");
	private final JButton redoButton = new JButton("恢复撤销");
	private final JButton exitButton = new JButton("退出");

	SketchpadPanel sketchPanel = new SketchpadPanel();

	private void initFrame() {
		commandPanel.setLayout(new FlowLayout());
		commandPanel.add(lineButton);
		commandPanel.add(circleButton);
		commandPanel.add(rectangleButton);
		commandPanel.add(undoButton);
		commandPanel.add(redoButton);
		commandPanel.add(exitButton);

		colorPanel.setLayout(new FlowLayout());
		colorPanel.add(redButton);
		colorPanel.add(blueButton);
		colorPanel.add(greenButton);

		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(commandPanel, BorderLayout.NORTH);
		mainPanel.add(colorPanel, BorderLayout.CENTER);

		getContentPane().add("South", mainPanel);
		getContentPane().add("Center", sketchPanel);

		// 初始化设置:颜色和命令
		lineButton.setForeground(Color.RED);
		sketchPanel.setColor(Color.RED);
		redButton.setForeground(Color.RED);

		sketchPanel.setCommand(ShapeCommand.LINE);
	}

	private void initListener() {
		redButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				redAction(e);
			}
		});
		blueButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				blueAction(e);
			}
		});
		greenButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				greenAction(e);
			}
		});

		undoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				undoAction(e);
			}
		});
		redoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				redoAction(e);
			}
		});
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				exitAction(e);
			}
		});

		lineButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lineAction(e);
			}
		});
		circleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				circleAction(e);
			}
		});
		rectangleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				rectangleAction(e);
			}
		});
	}

	public SketchpadFrame() {
		initFrame();
		initListener();
		this.setSize(500, 600);
		setLocationByPlatform(true);
		setResizable(true);
	}

	/** ********************处理事件********************* */
	private void undoAction(ActionEvent e) {
		sketchPanel.undo();
	}

	private void redoAction(ActionEvent e) {
		sketchPanel.redo();
	}

	private void exitAction(ActionEvent e) {
		System.exit(0);
	}

	private void lineAction(ActionEvent e) {
		// 选中按钮为红色, 其余为黑色
		lineButton.setForeground(Color.RED);
		circleButton.setForeground(Color.BLACK);
		rectangleButton.setForeground(Color.BLACK);
		sketchPanel.setCommand(ShapeCommand.LINE);
	}

	private void circleAction(ActionEvent e) {
		circleButton.setForeground(Color.RED);
		lineButton.setForeground(Color.BLACK);
		rectangleButton.setForeground(Color.BLACK);
		sketchPanel.setCommand(ShapeCommand.CIRCLE);
	}

	private void rectangleAction(ActionEvent e) {
		rectangleButton.setForeground(Color.RED);
		lineButton.setForeground(Color.BLACK);
		circleButton.setForeground(Color.BLACK);
		sketchPanel.setCommand(ShapeCommand.RECTANGLE);
	}

	private void redAction(ActionEvent e) {
		redButton.setForeground(Color.RED);
		blueButton.setForeground(Color.BLACK);
		greenButton.setForeground(Color.BLACK);
		sketchPanel.setColor(Color.RED);

	}

	private void blueAction(ActionEvent e) {
		blueButton.setForeground(Color.RED);
		redButton.setForeground(Color.BLACK);
		greenButton.setForeground(Color.BLACK);
		sketchPanel.setColor(Color.BLUE);

	}

	private void greenAction(ActionEvent e) {
		greenButton.setForeground(Color.RED);
		redButton.setForeground(Color.BLACK);
		blueButton.setForeground(Color.BLACK);
		sketchPanel.setColor(Color.GREEN);

	}
}
